package com.youths.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.youths.dao.LabelConsumerMapper;
import com.youths.domain.LabelConsumer;
import com.youths.service.LoginService;
import com.youths.util.MD5Util;
import com.youths.dao.LabelAdminMapper;
import com.youths.domain.LabelAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.provider.MD5;

import java.util.List;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired(required = false)
    LabelAdminMapper labelAdminMapper;

    @Autowired(required = false)
    LabelConsumerMapper labelConsumerMapper;

    @Override
    public JSONObject adminLogin(String account, String password) {
        String passwordMd5 = MD5Util.MD5("youths" + password);
        JSONObject jsonObject = new JSONObject();
        List<LabelAdmin> admin = labelAdminMapper.login(account, passwordMd5);
        if(admin != null && !admin.isEmpty()){
            jsonObject.put("check","success");
            if(admin.get(0).getPower() == 2 || admin.get(0).getPower() == 3){
                jsonObject.put("role","super");
            }else{
                jsonObject.put("role","admin");
            }
            jsonObject.put("user",admin.get(0));
            return jsonObject;
        }
        jsonObject.put("check","fail");
        return jsonObject;
    }


    @Override
    public LabelConsumer consumerLogin(String phone, String password) {
        String passwordMd5 = MD5Util.MD5("youths" + password);
        return labelConsumerMapper.login(phone, passwordMd5);
    }

    @Override
    public int updatePassword(Integer id, String password) {
        return labelConsumerMapper.updatePassword(id, password);
    }
}
